package com.yz.jdd.widget;



import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.yz.jdd.R;

public class FlowIndicator extends View {
	private int count;
	private float space, radius;

	// 设置选中为 矩形
	private boolean selectRect;

	// 设置未选中为 矩形
	private boolean normalRect;

	public void setDrawRound(boolean drawRound) {
		this.drawRound = drawRound;
	}

	//画不画圆角
	private boolean drawRound;

	// 选中矩形高度
	private int selectRectHight = 0;

	private String showType;

	private int selectRectWidth;

	public void setShowType(String showType) {
		this.showType = showType;
	}

	public void setSelectRectHight(int selectRectHight) {
		this.selectRectHight = selectRectHight;
	}

	public void setNormalRect(boolean normalRect) {
		this.normalRect = normalRect;
	}

	public void setSelectRect(boolean selectRect) {
		this.selectRect = selectRect;
	}

	public void setSpace(float space) {
		this.space = space;
	}

	public void setRadius(float radius) {
		this.radius = radius;
	}

	public void setPoint_normal_color(int point_normal_color) {
		this.point_normal_color = point_normal_color;
	}

	public void setPoint_seleted_color(int point_seleted_color) {
		this.point_seleted_color = point_seleted_color;
	}

	public void setSeleted(int seleted) {
		this.seleted = seleted;
	}

	private int point_normal_color, point_seleted_color;

	// 选中
	private int seleted = 0;

	// background seleted normal

	public FlowIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.FlowIndicator);

		// count = a.getInteger(R.styleable.FlowIndicator_count, 10);
		space = a.getDimension(R.styleable.FlowIndicator_space, 9);
		radius = a.getDimension(R.styleable.FlowIndicator_point_radius, 9);

		point_normal_color = a.getColor(
				R.styleable.FlowIndicator_point_normal_color, 0x000000);
		point_seleted_color = a.getColor(
				R.styleable.FlowIndicator_point_seleted_color, 0xffff07);

		int sum = attrs.getAttributeCount();
		if (true) {
			String str = "";
			for (int i = 0; i < sum; i++) {
				String name = attrs.getAttributeName(i);
				String value = attrs.getAttributeValue(i);
				str += "attr_name :" + name + ": " + value + "\n";
			}
		}
		a.recycle();
	}

	public void setSeletion(int index) {
		this.seleted = index;
		invalidate();
	}

	public void setCount(int count) {
		this.count = count;
		invalidate();
	}

	public void next() {
		if (seleted < count - 1)
			seleted++;
		else
			seleted = 0;
		invalidate();
	}

	public void previous() {
		if (seleted > 0)
			seleted--;
		else
			seleted = count - 1;
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint paint = new Paint();
		paint.setAntiAlias(true);

		float width = (getWidth() - ((radius * 2 * count) + selectRectWidth+(space * (count - 1))))/2;
//		 float width=0;
		for (int i = 0; i < count; i++) {

//			if (true) {
				//选中状态是矩形，
//				if (i == seleted) {
//					paint.setColor(point_seleted_color);
//					//选中，画矩形，
//					if (selectRect) {
//						//选中状态是矩形
//							if(drawRound){
//								//带圆角
//								canvas.drawRoundRect(new RectF(
//												getPaddingLeft() + i* (radius * 2)+space * i,
//												0,
//												getPaddingLeft() + i* (radius * 2)+space * i+radius * 2+selectRectWidth,
//												getHeight()),
//										radius, radius, paint);
//							}else{
//								canvas.drawRect(new RectF(
//										getPaddingLeft() + i* (radius * 2)+space * i,
//										0,
//										getPaddingLeft() + i* (radius * 2)+space * i+radius * 2+selectRectWidth,
//										getHeight()),paint);
//							}
//
//
//					} else {
//						//选中状态不是矩形,是圆形
//						canvas.drawCircle(width + getPaddingLeft() + radius + i
//										* (space + radius + radius), getHeight() / 2,
//								radius , paint);
//					}
//
//				} else {
//					paint.setColor(point_normal_color);
//					if (normalRect) {
//						//未选中状态也是矩形，全部画矩形
//						if(drawRound){
//							//带圆角
//							canvas.drawRoundRect(new RectF(
//											getPaddingLeft() + i* (radius * 2)+space * i,
//											0,
//											getPaddingLeft() + i* (radius * 2)+space * i+radius * 2+selectRectWidth,
//											getHeight()),
//									radius, radius, paint);
//						}else{
//							canvas.drawRect(new RectF(
//									getPaddingLeft() + i* (radius * 2)+space * i,
//									0,
//									getPaddingLeft() + i* (radius * 2)+space * i+radius * 2+selectRectWidth,
//									getHeight()),paint);
//						}
//
//
//					} else {
//						//未选中状态不是矩形,是圆形
////						canvas.drawCircle(width + getPaddingLeft() + radius + i
////										* (space + radius + radius), getHeight() / 2,
////								radius , paint);
//						//选中前的，选中后的
//						paint.setColor(point_normal_color);
//						if(seleted>i){
//							canvas.drawCircle(getPaddingLeft() + radius*2*i + radius+i+space+selectRectWidth
//									, getHeight() / 2,
//									radius , paint);
//						}else  if(seleted<i){
//							canvas.drawCircle(getPaddingLeft() + radius*2*i + radius+i+space
//											, getHeight() / 2,
//									radius , paint);
//						}
//					}
//
//				}

//			} else {
				if (i == seleted) {
					paint.setColor(point_seleted_color);
				} else {
					paint.setColor(point_normal_color);
				}
				canvas.drawCircle(width + getPaddingLeft() + radius + i
						* (space + radius + radius), getHeight() / 2,
						radius - 2, paint);
//
			}
//
//		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(measureWidth(widthMeasureSpec),
				measureHeight(heightMeasureSpec));
	}

	private int measureWidth(int measureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		if (specMode == MeasureSpec.EXACTLY) {
			result = specSize;
		} else {
			result = (int) (getPaddingLeft() + getPaddingRight()
					+ (count * 2 * radius) + (count - 1) * radius + 1);
			if (specMode == MeasureSpec.AT_MOST) {
				result = Math.max(result, specSize);
			}
		}
		return result;
	}

	private int measureHeight(int measureSpec) {
		int result = 0;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);

		if (specMode == MeasureSpec.EXACTLY) {
			result = specSize;
		} else {
			result = (int) (2 * radius + getPaddingTop() + getPaddingBottom() + 1);
			if (specMode == MeasureSpec.AT_MOST) {
				result = Math.min(result, specSize);
			}
		}
		return result;
	}

}
